//
//  ViewController.m
//  自动生成请求模型
//
//  Created by tanxk_mini on 2019/5/20.
//  Copyright © 2019 tanxk. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (weak) IBOutlet NSTextFieldCell *urlTextField;
@property (unsafe_unretained) IBOutlet NSTextView *textView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];
    
}

#pragma mark - Action

- (IBAction)startClicked:(NSButton *)sender {
    
    NSLog(@"开始请求");
    NSURL *url2 = [NSURL URLWithString:self.urlTextField.title];
    NSData *jsonData = [NSData dataWithContentsOfURL:url2];
    if (jsonData == nil) {
        NSLog(@"开始失败");
        return;
    }
    
    NSDictionary *appVersion = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
//    [self mdf_creatPropertyCodeWith:appVersion];
    NSLog(@"------");
//    [self mdf_creatPropertyCodeWith:appVersion[@"tags"]];
    NSLog(@"------");
    [self mdf_creatPropertyCodeWith:appVersion[@"paths"][@"/market/channel/restuser/regCount"][@"post"]];
    
    self.textView.string = [NSString stringWithFormat:@"%@",appVersion];
}

#pragma mark - Work
/* 使用实例
 [self mdf_creatPropertyCodeWith:item.responseObject[@"resultObj"]];
 */
- (void)mdf_creatPropertyCodeWith:(NSDictionary *)dict {
    
    if ([dict isKindOfClass:[NSArray class]]) {
        dict = ((NSArray *)dict).firstObject;
    }
    
    //获取最大匿名函数长度，作用：对齐
    __block NSUInteger maxLength = 0;
    [dict enumerateKeysAndObjectsUsingBlock:^(NSString  * _Nonnull propertyName, id  _Nonnull obj, BOOL * _Nonnull stop) {
        if (propertyName.length >= maxLength) {
            maxLength = propertyName.length;
        }
    }];
    
    NSMutableString *strM = [NSMutableString string];
    
    [dict  enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull propertyName, id  _Nonnull value, BOOL * _Nonnull stop) {
        NSString *code;
        if ([value isKindOfClass:[NSString class]]) {
            value = [(NSString *)value stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
        }
        if ([value isKindOfClass:NSClassFromString(@"__NSCFString")])
        {
            code=[NSString stringWithFormat:@"@property (nonatomic,copy) NSString *%@;%@///< %@",propertyName,createSpace(maxLength, propertyName),createValue(value)];
        }
        else if ([value isKindOfClass:NSClassFromString(@"__NSCFNumber")])
        {
            code=[NSString stringWithFormat:@"@property (nonatomic,copy) NSString *%@;%@///< %@",propertyName,createSpace(maxLength, propertyName),createValue(value)];
        }
        else if ([value isKindOfClass:NSClassFromString(@"__NSArrayI")])
        {
            code=[NSString stringWithFormat:@"@property (nonatomic,strong) NSArray *%@;%@///< %@",propertyName,createSpace(maxLength, propertyName),createValue(value)];
        }
        else if ([value isKindOfClass:NSClassFromString(@"__NSDictionary")])
        {
            code=[NSString stringWithFormat:@"@property (nonatomic,strong) NSDictionary %@;%@///< %@",propertyName,createSpace(maxLength, propertyName),createValue(value)];
        }
        else if ([value isKindOfClass:NSClassFromString(@"NSTaggedPointerString")])
        {
            code=[NSString stringWithFormat:@"@property (nonatomic,copy) NSString *%@;%@///< %@",propertyName,createSpace(maxLength, propertyName),createValue(value)];
        }
        else if ([value isKindOfClass:NSClassFromString(@"__NSCFConstantString")])
        {
            code=[NSString stringWithFormat:@"@property (nonatomic,copy) NSString *%@;%@///< %@",propertyName,createSpace(maxLength, propertyName),createValue(value)];
        }
        else if ([value isKindOfClass:NSClassFromString(@"NSTaggedPointerString")])
        {
            code=[NSString stringWithFormat:@"@property (nonatomic,copy) NSString *%@;%@///< %@",propertyName,createSpace(maxLength, propertyName),createValue(value)];
        }
        else if ([value isKindOfClass:NSNull.class])
        {
            code=[NSString stringWithFormat:@"@property (nonatomic,copy) NSString *%@;%@///< %@",propertyName,createSpace(maxLength, propertyName),createValue(value)];
        }
        else if ([value isKindOfClass:NSClassFromString(@"__NSDictionaryM")])
        {
            code=[NSString stringWithFormat:@"@property (nonatomic,copy) NSMutableDictionary *%@;%@///< %@",propertyName,createSpace(maxLength, propertyName),createValue(value)];
        }
        else if ([value isKindOfClass:NSClassFromString(@"__NSCFBoolean")])
        {
            code=[NSString stringWithFormat:@"@property (nonatomic,assign) BOOL *%@;%@///< %@",propertyName,createSpace(maxLength, propertyName),createValue(value)];
        }
        else if ([value isKindOfClass:NSClassFromString(@"__NSArrayM")])
        {
            code=[NSString stringWithFormat:@"@property (nonatomic,copy) NSMutableArray *%@;%@///< %@",propertyName,createSpace(maxLength, propertyName),createValue(value)];
        }
        else
        {
            NSString *method =  NSStringFromClass([value class]);
            NSString *tempCode = [NSString stringWithFormat:@"@property (nonatomic,copy) %@ *%@; ///< %@",method, propertyName, @""];
            NSLog(@"需要添加:%@",tempCode);
            NSAssert(value, @"这是什么鬼");
        }
        [strM appendFormat:@"\n%@",code];
    }];
    
    
    
    NSLog(@"%@",strM);
}

NSString *createSpace(NSUInteger length, NSString *currentPropertyName) {
    
    NSUInteger tempLength = length - currentPropertyName.length;

    NSMutableString *mString = [NSMutableString string];
    while (tempLength > 0) {
        [mString appendString:@" "];
        tempLength = tempLength-1;
    }
    return mString;
}

NSString *createValue(id value) {
    
    NSString *string = [NSString stringWithFormat:@"%@",value];
    string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (string.length > 200) {
        string = [string substringToIndex:200];
    }
    return string;
}

@end
